package br.ufrj.dcc.dojo.jtrace.texture;

import java.io.IOException;

import org.jtrace.primitives.Point2D;
import org.jtrace.primitives.Vector3D;

public class SphereTextureShader extends TextureShader {

	public SphereTextureShader(String filename) throws IOException {
		super(filename);
	}

	@Override
	public Point2D getPoint2DFromMapping(Vector3D vector, double height) {
		final double PI = 3.141592654;
    	final double TWOPI = 6.283185308;
    	
    	double radius = vector.module();
    	double v = Math.acos(vector.getZ()/radius) / PI;
    	double u;
    	double bagulho = Math.acos(vector.getX() / (radius * Math.sin(PI * v)));
    	if (vector.getY() >= 0) {
			u = bagulho / TWOPI;
    	} else {
    		u = 1 - (bagulho / TWOPI) ;
    	}

		return new Point2D(u, v);
	}

}